from rest_framework.views import exception_handler
from luffyapi.utils.response import APIResponse
from luffyapi.utils.logger import log


def common_exception_handler(exc, context):
    log.error(f'error class is {context["view"].__class__.__name__}, Error massage is [{str(exc)}]')
    response = exception_handler(exc, context)
    if not response:
        if isinstance(exc, KeyError):
            return APIResponse(code=0, msg='key error')
        return APIResponse(code=0, msg='error', result=str(exec))
    else:
        return APIResponse(code=0, msg='error', result=response.data)
